﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using VisualLib;
using DTBuilderLib;
using Logging;

namespace DeepZoomImageInset
{
    /// <summary>
    /// A simple program that puts one image inside the other (makes an in-set).
    /// This is only done for one image, and it is done as a single DZI (in a collection).
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            ///
            /// Make sure we can find the actual image!
            /// 

            FileInfo masterImage = new FileInfo("master.png");
            if (!masterImage.Exists)
            {
                Console.WriteLine("Can't find {0}!", masterImage.Name);
                return;
            }
            FileInfo insetImage = new FileInfo("inset.png");
            if (!insetImage.Exists)
            {
                Console.WriteLine("Can't find {0}!", insetImage.Name);
                return;
            }

            ///
            /// Create the visuals that will hold the two images we are going
            /// to play with. For sanity's sake, keep them all at the same DPI.
            /// 

            VisualSingleImage vMaster = new VisualSingleImage(masterImage, 150);
            Console.WriteLine("Master image size is (x,y) = ({0}, {1})", vMaster.Width, vMaster.Height);
            VisualSingleImage vInset = new VisualSingleImage(insetImage, 150);
            Console.WriteLine("Inset image size is (x,y) = ({0}, {1})", vInset.Width, vInset.Height);

            ///
            /// Now, place them inside each other. so, we will have the inset image be 10% of the size of the master
            /// image, and have it set 10% from the bottom and right of the master image. In order to make things
            /// nicer (a bit) we rescale. Simple, but tedious math ahead.
            /// 
            /// 1) Scale master to be 1.0 and the inset to be 10% of that.
            /// 2) Set the x of the inset to be x% over (so it can be placed on top of the other guy).
            ///     The math here is so that I can easily play with the offset to get it right!
            /// 3) calc the proper y position (because this image may not be square, we cna't just do 0.8).
            /// 4) Set the z index correctly
            /// 

            double xoffsetFromEdge = 0.12;
            double yoffsetFromEdge = 0.67;

            vMaster.ScaleToWidth(1.0);
            vInset.ScaleToWidth(0.1);

            vInset.XPosition = 1.0 - (vInset.Width + xoffsetFromEdge);
            vInset.YPosition = vMaster.Height * (1.0 - (vInset.Height + yoffsetFromEdge));

            ///
            /// Last thing to do is combine them into a single image using a holder that can deal with multiple
            /// (any number) of images. We increment the z index of the second image to make sure that it appears in
            /// fron tof the first one!
            /// 
            /// This holder will be the completed picture, and the one we render.
            /// 

            VisualHolder h = new VisualHolder();
            h.AddImage(vMaster);
            vInset.IncZIndex();
            h.AddImage(vInset);

            ///
            /// Ok. Now make a deep zoom image out of this guy!
            /// 0) We pass a console logger so errors can be written to stdout (exceptions also tend to get thrown!)
            /// 1) We set the directory where we want this guy to be output!
            ///    We use "output" so that we can keep our source directory nice and clean!
            /// 2) We set the image quality. This is directly fed to the DZ controls and sets 

            DeepZoomBuilder builder = new DeepZoomBuilder(new LoggerConsole());
            var result = builder.RenderVisual(new DirectoryInfo(".\\Inset Image Test"), h);
            Console.WriteLine("The final result can be found in {0}", result.FullName);
        }
    }
}
